package com.hod.client.room;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.gears.client.database.DatabaseException;
import com.google.gwt.gears.client.database.ResultSet;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.core.Function;
import com.gwtext.client.core.Position;
import com.gwtext.client.data.ArrayReader;
import com.gwtext.client.data.MemoryProxy;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.PaddedPanel;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Checkbox;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Label;
import com.gwtext.client.widgets.form.NumberField;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.grid.AbstractSelectionModel;
import com.gwtext.client.widgets.grid.RowSelectionModel;
import com.gwtext.client.widgets.grid.event.RowSelectionListenerAdapter;
import com.gwtext.client.widgets.layout.ColumnLayout;
import com.gwtext.client.widgets.layout.ColumnLayoutData;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.layout.FormLayout;
import com.gwtext.client.widgets.layout.RowLayout;
import com.hod.client.HOD;
import com.hod.client.backend.orm.client.GearsDataStore;
import com.hod.client.backend.orm.client.GearsQuery;
import com.hod.client.bed.Bed;
import com.hod.client.bed.BedGrid;
import com.hod.client.client.ClientContactData;
import com.hod.client.client.ClientContactDataGrid;
import com.hod.client.frontend.AbstractGrid;
import com.hod.client.frontend.CRUD;
import com.hod.client.frontend.HODForm;
import com.hod.client.lodging.Lodging;
import com.hod.client.roomtype.RoomType;
import com.hod.client.roomtype.RoomTypeCRUD;
import com.hod.client.roomtype.RoomTypeRecordset;


public class RoomCRUD extends CRUD {
 
	protected String idRoomType;
	protected String idRoom = "1";
    private GearsDataStore dsBedGrid = (GearsDataStore) GWT.create(Bed.class);
    protected BedGrid bedGrid = new BedGrid(false,dsBedGrid,idRoom);
	
    
    
    
	public RoomCRUD() {
		ds = (GearsDataStore) GWT.create(Room.class);
		gridPanel = new RoomGrid(false,ds);		
	}
		
	@Override
	protected void buildPanelSearch() {
		// TODO Auto-generated method stub
/*		Label lblTipoHabitacion =  new Label("Tipo de habitacion");
        final ListBox lbTipoHabitacion = new ListBox();
        HorizontalPanel panelTipoHabitacion = new HorizontalPanel();
        panelTipoHabitacion.add(lblTipoHabitacion);
        panelTipoHabitacion.add(lbTipoHabitacion);
        for(RoomType rt: RoomTypeCRUD.getRoomTypes()){
        	lbTipoHabitacion.addItem(rt.getName(),String.valueOf(rt.getIdRoomType()));
        }
        formPanel.add(panelTipoHabitacion);*/
		
	}


	@Override
	protected AbstractSelectionModel createSelectionModel() {
		// TODO Auto-generated method stub
		final RowSelectionModel sm = new RowSelectionModel(true);
        sm.addListener(new RowSelectionListenerAdapter() {
        	public void onRowSelect(RowSelectionModel sm, int rowIndex, Record record) {
        		try {
        			formPanel.updateDataControls(record);
					idRoom = record.getAsString("idRoom");
					bedGrid.refresh(idRoom);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					Window.alert(e.getMessage());
				}
            }
        });
        return sm;
	}


	@Override
	protected Widget formTabs() {
		// TODO Auto-generated method stub
		final TabPanel tabPanel = new TabPanel();
		Panel bedData = new Panel(); 
		
        tabPanel.setActiveTab(0);
               
        bedData.setTitle("Beds");
        bedData.setLayout(new FormLayout());
        bedData.setAutoHeight(true);
        bedData.setPaddings(10);
        bedData.add(bedGrid);

        tabPanel.add(bedData);
        
        
        
        return tabPanel;
	}

	@Override
	public HODForm createFormPanel() {
		// TODO Auto-generated method stub
		if (formPanel==null) formPanel = new RoomFormPanel(ds);
		return formPanel;
	}


	
	

}
